var layer, table, form;
layui.use(['layer', 'laydate', 'table', 'form'], function () {
    layer = layui.layer;
    laydate = layui.laydate;
    table = layui.table;
    form = layui.form;


    //自定义验证规则
    form.verify({
        dormitoryId: function (value) {
            if (value == '') {
                return '请填写宿舍编号';
            }
        },
        dormitoryName: function (value) {
            if (value == '') {
                return '请填写宿舍名称';
            }
        }
    });
    //监听提交
    form.on('submit(form)', function (data) {
        var param = data.field;

        var isModify = $('#isModify').val();
        if (isModify === 'true') {
            modifyDormitory(param);
        } else {
            addDormitory(param);
        }
        return false;
    });
});

function addDormitory(param) {
    $.ajax({
        type: "POST",
        url: "/dormitory/addDormitory",
        contentType: "application/json",
        sync: true,
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            console.log(result)
            if (result.code == 200) {
                // 弹出提示
                layer.msg('添加宿舍成功！', {anim: 3, icon: 6, time: 2000, shade: 0.1});
                setTimeout(function () {
                    // 关闭自身窗口
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    // 刷新父窗口
                    parent.layui.table.reload('dormitoryTable');//重新加载父页面中的数据表格
                }, 1000);
            } else {
                layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
            }
        }
    });
}

function modifyDormitory(param) {
    $.ajax({
        type: "POST",
        url: "/dormitory/modifyDormitory",
        contentType: "application/json",
        sync: true,
        data: JSON.stringify(param),
        dataType: "json",
        success: function (result) {
            console.log(result)
            if (result.code == 200) {
                // 弹出提示
                layer.msg('修改宿舍信息成功！', {anim: 3, icon: 6, time: 2000, shade: 0.1});
                setTimeout(function () {
                    // 关闭自身窗口
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    // 刷新父窗口
                    parent.layui.table.reload('dormitoryTable');//重新加载父页面中的数据表格
                }, 1000);
            } else {
                layer.msg(result.msg, {icon: 2, offset: '100px', time: 2000});
            }
        }
    });
}

/* 监测学号输入框，判断该是否已经录入 */
function checkDoemitoryIsExisted() {
    var isModify = $('#isModify').val();
    if (isModify === 'true') {
        return;
    }

    var param = {
        stuId: $('#stuId').val()
    };
    $.ajax({
        type: "POST",
        url: "/dormitory/checkDoemitoryIsExisted",
        //contentType: "application/json",
        sync: true,
        data: param,
        dataType: "json",
        success: function (result) {
            // 该学生已录入
            if (result.data == true) {
                layer.msg('该学生已存在！', {anim: 6, icon: 5, time: 1500, shade: 0.1});
                $('#stuId').val('');
            }
        }
    });
}